/**
 * 
 */
var thispage = {
		init: function(){
			this.tableInit();
			this.funs.initMenuTree();
			this.initObj();
			this.initEvents();
		},
		initObj: function(){
			this.zTreeObj = null;
		},
		initEvents: function(){
			var t = this;
			$('.d-tbl .layui-btn').on('click', function() {
				var type = $(this).attr('@event');
				t.funs[type] ? t.funs[type].call(this) : '';
			});
		},
		funs: {
			add: function(){
				pub.openEdit("新增系统菜单", ctx + "/menu/edit", [ '420px', '355px' ]);
			},
			//删除行
			editRow: function(rowId){
				pub.openEdit("修改系统菜单", ctx + "/menu/edit?id=" + rowId, [ '420px', '355px' ]);
			},
			//编辑行
			delRow: function(rowId){
				layer.confirm('确定要删除菜单吗', function(index) {
					layer.close(index);
					//向服务端发送删除指令
					$.post(ctx +"/menu/delete", {id: rowId}, function(ret){
						if(ret.state == "ok"){
							thispage.funs.search();
							thispage.funs.initMenuTree();
						}else{
							layer.alert(ret.msg || "服务器异常");
						}
					})
				});
			},
			search:  function(where){
				//执行重载
				$('#tblmain').bootstrapTable('refresh', {query: where || {}});
			},
			initMenuTree: function(){
				//选中节点菜单时
				this.menuTreeBeforeClick = function(treeId, treeNode, clickFlag){
					thispage.funs.search({"longNo" : treeNode.longNo});
				}
				var setting = {
						async : {
							enable: true,
							url: ctx + "/menu/tree",
						},
						callback: {
							beforeClick: this.menuTreeBeforeClick
						},
						data: {
							simpleData: {
								enable: true
							}
						}
				};
				if(thispage.zTreeObj){
					$.fn.zTree.destroy("menu-tree");
					thispage.zTreeObj = null;
				}
				thispage.zTreeObj = $.fn.zTree.init($("#menu-tree"), setting, []);
			}
		},
		tableInit: function(){
			var t = this;
			var maxH = document.documentElement.clientHeight;
			var height = maxH;
			var columns = [{
				field: 'id',
				title: 'ID',
				visible: false
			},{
				field: 'menu_no',
				title: '菜单编码',
				sortable: true
			}, {
				field: 'menu_name',
				title: '菜单名称',
				sortable: true
			}, {
				field: 'menu_icon',
				title: '菜单图标',
				align: "center",
				sortable: false,
				formatter: function(value, row, rowIndex){
					return '<i class="iconfont '+value+'"</i>'
				}
			}, {
				field: 'long_no',
				title: '长编码',
				sortable: true
			}, {
				field: 'isleaf',
				title: '叶子节点',
				sortable: true
			}, {
				field: 'createtime',
				title: '创建时间',
				sortable: true
			}, {
				field: "option",
				title: "操作",
				align: "center",
				formatter: function(value, row, rowIndex){
					return "<span class='option glyphicon glyphicon-edit' aria-hidden='true' onclick=thispage.funs.editRow('"+row.id+"') ></span>" +
						   "<span class='option glyphicon glyphicon-remove' aria-hidden='true' onclick=thispage.funs.delRow('"+row.id+"') ></span>";
				}
			}];
			var tableParams = $.extend(tableDefault.params, {
				url: ctx + '/menu/query',
				sortOrder: "asc",   //排序方式
				sortName: "long_no", //默认排序字段
				columns: columns,
				height: height,   //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
				onDblClickRow: function(row, $elemen, field){
					thispage.funs.editRow(row.id);
				}
			});
			$('#tblmain').bootstrapTable(tableParams);
		}
}


layui.use([ 'layer' ], function() {
	var table = layui.table;
	var layer = layui.layer;
	thispage.init();

	//提供给editMenu 调用
	window.events = {
			search: function(){
				thispage.funs.search();
			},
			initMenuTree: function(){
				thispage.funs.initMenuTree();
			}
	}
});
